package com.zoe.onelink.governance.gateway.model;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.zoe.onelink.governance.gateway.serializer.GatewayRouteParamSerializer;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>标题: Spring Cloud Gateway 路由原生格式</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-10-20
 */
@Data
@Accessors(chain = true)
public class NativeGatewayRoute {

    public static final String GENERATED_NAME_PREFIX = "_genkey_";

    /**
     * 唯一ID标识
     */
    private String md5;
    /**
     * 路由ID
     */
    private String id;
    /**
     * 要转发的URI,例如 lb://test-service
     */
    private String uri;
    /**
     * 路由排序
     */
    private int order;
    /**
     * 断言
     */
    private List<PredicatesDTO> predicates = new ArrayList<>();
    /**
     * 过滤器
     */
    private List<FiltersDTO> filters = new ArrayList<>();
    /**
     * 元数据
     */
    private Map<String, Object> metadata = new HashMap<>();

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        NativeGatewayRoute that = (NativeGatewayRoute) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Data
    @JsonSerialize(using = GatewayRouteParamSerializer.class)
    public static class FiltersDTO implements GatewayRouteParam {

        public FiltersDTO() {
        }

        public FiltersDTO(String text) {
            int eqIdx = text.indexOf('=');
            if (eqIdx <= 0) {
                throw new IllegalArgumentException(
                        "Unable to parse PredicateDefinition text '" + text + "'" + ", must be of the form name=value");
            }
            setName(text.substring(0, eqIdx));

            String[] args = StringUtils.tokenizeToStringArray(text.substring(eqIdx + 1), ",");

            for (int i = 0; i < args.length; i++) {
                this.args.put(GENERATED_NAME_PREFIX + i, args[i]);
            }
        }

        private Map<String, String> args = new LinkedHashMap<>();
        private String name;
    }

    @Data
    @JsonSerialize(using = GatewayRouteParamSerializer.class)
    public static class PredicatesDTO implements GatewayRouteParam {

        public PredicatesDTO() {
        }

        public PredicatesDTO(String text) {
            int eqIdx = text.indexOf('=');
            if (eqIdx <= 0) {
                setName(text);
                return;
            }
            setName(text.substring(0, eqIdx));

            String[] args = StringUtils.tokenizeToStringArray(text.substring(eqIdx + 1), ",");

            for (int i = 0; i < args.length; i++) {
                this.args.put(GENERATED_NAME_PREFIX + i, args[i]);
            }
        }

        private Map<String, String> args = new LinkedHashMap<>();
        private String name;
    }

    public interface GatewayRouteParam {

        Map<String, String> getArgs();

        String getName();

    }

}